﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using ScreenSharing;
using ScreenSharing.sdk;

namespace SmartConference.SmartPlayer
{
    public delegate void ContionStatusChange(NTSmartPlayerDefine.NT_SP_E_EVENT_ID code);
    public partial class PlayerManager
    {
        #region MyRegion
        [DllImport("kernel32", EntryPoint = "CopyMemory")]
        static extern void CopyMemory(IntPtr Destination, IntPtr Source, uint Length);

        //分辨率信息回调
        delegate void ResolutionNotifyCallback(Int32 width, Int32 height);
        delegate void VideoFrameCallBack(UInt32 status, NT_SP_VideoFrame frame);
        delegate void AudioPCMFrameCallBack(UInt32 status, IntPtr data, UInt32 size,
            Int32 sample_rate, Int32 channel, Int32 per_channel_sample_number);
        delegate void SetRenderVideoFrameTimestampCallBack(UInt64 timestamp, UInt64 reserve1, IntPtr reserve2);
        delegate void SetCaptureImageCallBack(UInt32 result, [MarshalAs(UnmanagedType.LPStr)] String file_name);
        delegate void SetRecordCallBack(UInt32 status, [MarshalAs(UnmanagedType.LPStr)] String file_name);
        delegate void SetEventCallBack(UInt32 event_id,
            Int64 param1,
            Int64 param2,
            UInt64 param3,
            [MarshalAs(UnmanagedType.LPStr)] String param4,
            [MarshalAs(UnmanagedType.LPStr)] String param5,
            IntPtr param6);
        delegate void SetUserDataCallBack(UInt32 data_type,
            string data,
            UInt32 size,
            UInt64 timestamp,
            UInt64 reserve1,
            Int64 reserve2,
            IntPtr reserve3);

        public event ContionStatusChange ContionStatusChange;
        //快照截图配置
        private String capture_image_path_ = "";

        //record配置
        private String rec_dir_ = "";
        private String rec_name_file_prefix_ = "";
        private UInt32 max_file_size_ = 200;
        private bool is_append_date_ = true;
        private bool is_append_time_ = true;

        private IntPtr player_handle_;

        private Int32 width_ = 0;
        private Int32 height_ = 0;

        public bool is_sdk_init_ = false;
        private bool is_playing_ = false;
        private bool is_recording_ = false;

        private UInt32 connection_status_ = 0;
        private UInt32 buffer_status_ = 0;
        private Int32 buffer_percent_ = 0;
        private Int32 download_speed_ = -1;

        private Int32 rotate_degrees_ = 0;

        private System.Timers.Timer timer_clock_;

        SP_SDKVideoSizeCallBack video_size_call_back_;

        private NT_SP_VideoFrame cur_video_frame_ = new NT_SP_VideoFrame();

        bool is_gdi_render_ = false;

        ResolutionNotifyCallback resolution_notify_callback_;

        //视频数据回调
        SP_SDKVideoFrameCallBack video_frame_call_back_;
        

        //delegate void VideoFrameCallBack(UInt32 status, Bitmap bitmap, int width, int height);
        VideoFrameCallBack set_video_frame_call_back_;

        //视频数据回调
        SP_SDKAudioPCMFrameCallBack audio_pcm_frame_call_back_;
        
        AudioPCMFrameCallBack set_audio_pcm_frame_call_back_;

        //视频时间戳回调
        SP_SDKRenderVideoFrameTimestampCallBack video_frame_ts_callback_;
        
        SetRenderVideoFrameTimestampCallBack set_render_video_frame_timestamp_callback_;

        //快照信息回调
        SP_SDKCaptureImageCallBack capture_image_call_back_;
        
        SetCaptureImageCallBack set_capture_image_call_back_;

        //录像信息回调
        SP_SDKRecorderCallBack record_call_back_;
        
        SetRecordCallBack set_record_call_back_;

        //event事件回调
        SP_SDKEventCallBack event_call_back_;
       
        SetEventCallBack set_event_call_back_;

        //接收到的用户数据回调
        SP_SDKUserDataCallBack user_data_call_back_;
       

        SetUserDataCallBack set_user_data_call_back_; 
        #endregion

        private PictureBox playWnd;
        private string Url { get; set; }

        private  Form ManagerForm { get; }

        public PlayerManager(Form form,PictureBox pictureBox, string url)
        {
            try
            {
                ManagerForm = form;
                playWnd = pictureBox;
                Url = url;
                is_sdk_init_ = false;
                is_playing_ = false;

                resolution_notify_callback_ = PlaybackWindowResized;
                set_video_frame_call_back_ = SdkVideoFrameCallBack;
                set_audio_pcm_frame_call_back_ = SdkAudioPcmFrameCallBack;
                set_event_call_back_ = EventCallBack;

                UInt32 isInited = NTSmartPlayerSDK.NT_SP_Init(0, IntPtr.Zero);
                is_sdk_init_ = isInited == 0;
                NTSmartPlayerSDK.NT_SP_Open(out player_handle_, IntPtr.Zero, 0, IntPtr.Zero);
                event_call_back_ = SdkEventCallBack;
                NTSmartPlayerSDK.NT_SP_SetEventCallBack(player_handle_, IntPtr.Zero, event_call_back_);
            }
            catch (Exception ex)
            {
               Log4netHelper.Error(ex);
            }

        }


        #region Play
        public bool Play()
        {
            try
            {
                if (!InitCommonSdkParam()) return false;

                video_size_call_back_ = SP_SDKVideoSizeHandle;
                NTSmartPlayerSDK.NT_SP_SetVideoSizeCallBack(player_handle_, IntPtr.Zero, video_size_call_back_);

                var isSupportD3DRender = false;
                var inSupportD3DRender = 0;

                if (NTBaseCodeDefine.NT_ERC_OK == NTSmartPlayerSDK.NT_SP_IsSupportD3DRender(player_handle_, playWnd.Handle, ref inSupportD3DRender))
                    isSupportD3DRender = 1 == inSupportD3DRender;

                if (isSupportD3DRender)
                {
                    is_gdi_render_ = false;
                    // 支持d3d绘制的话，就用D3D绘制
                    NTSmartPlayerSDK.NT_SP_SetRenderWindow(player_handle_, playWnd.Handle);
                }
                else
                {
                    is_gdi_render_ = true;
                    playWnd.Visible = false;
                    video_frame_call_back_ = SetVideoFrameCallBack;
                    NTSmartPlayerSDK.NT_SP_SetVideoFrameCallBack(player_handle_, (Int32)NTSmartPlayerDefine.NT_SP_E_VIDEO_FRAME_FORMAT.NT_SP_E_VIDEO_FRAME_FORMAT_RGB32, IntPtr.Zero, video_frame_call_back_);
                }

                audio_pcm_frame_call_back_ = SetAudioPCMFrameCallBack;
                NTSmartPlayerSDK.NT_SP_SetAudioPCMFrameCallBack(player_handle_, IntPtr.Zero, audio_pcm_frame_call_back_);

                //video timestamp callback
                video_frame_ts_callback_ = SP_SDKRenderVideoFrameTimestampCallBack;
                NTSmartPlayerSDK.NT_SP_SetRenderVideoFrameTimestampCallBack(player_handle_, IntPtr.Zero, video_frame_ts_callback_);

                //设置是否播放出声音，如需自己播放PCM，可以设置第二个参数为0
                //NTSmartPlayerSDK.NT_SP_SetIsOutputAudioDevice(player_handle_, 0);

                user_data_call_back_ = SDKUserDataCallBack;
                NTSmartPlayerSDK.NT_SP_SetUserDataCallBack(player_handle_, IntPtr.Zero, user_data_call_back_);

                UInt32 ret_start = NTSmartPlayerSDK.NT_SP_StartPlay(player_handle_);

                if (ret_start != 0)
                    return false;
                is_playing_ = true;
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        public void Close()
        {
            StopPlayback();
        }


        private bool InitCommonSdkParam()
        {
            if (is_playing_) return false;
            if (IntPtr.Zero == player_handle_) return false;
            if (string.IsNullOrEmpty(Url)) return false;


            NTSmartPlayerSDK.NT_SP_SetBuffer(player_handle_, 100);

            // 设置rtsp tcp模式，rtmp不使用, 可以不设置
            NTSmartPlayerSDK.NT_SP_SetRTSPTcpMode(player_handle_, 0);

            //RTSP timeout设置
            Int32 rtsp_timeout = 10;
            NTSmartPlayerSDK.NT_SP_SetRtspTimeout(player_handle_, rtsp_timeout);

            //RTSP TCP/UDP自动切换设置
            Int32 is_auto_switch_tcp_udp = 1;
            NTSmartPlayerSDK.NT_SP_SetRtspAutoSwitchTcpUdp(player_handle_, is_auto_switch_tcp_udp);

            NTSmartPlayerSDK.NT_SP_SetMute(player_handle_, 0);

            NTSmartPlayerSDK.NT_SP_SetFastStartup(player_handle_, 1);

            // 设置低延迟模式
            NTSmartPlayerSDK.NT_SP_SetLowLatencyMode(player_handle_, 1);

            NTSmartPlayerSDK.NT_SP_SetRotation(player_handle_, rotate_degrees_);

            NTSmartPlayerSDK.NT_SP_SetReportDownloadSpeed(player_handle_, 1, 1);

            NTSmartPlayerSDK.NT_SP_SetURL(player_handle_, Url);

            return true;
        }

        private void SP_SDKVideoSizeHandle(IntPtr handle, IntPtr userData, Int32 width, Int32 height)
        {
            try
            {
                if (playWnd.InvokeRequired)
                {
                    playWnd.BeginInvoke(resolution_notify_callback_, width, height);
                }
                else
                {
                    resolution_notify_callback_(width, height);
                }
            }
            catch (Exception ex)
            {
               Log4netHelper.Error(ex);
            }
        }

        private void SetVideoFrameCallBack(IntPtr handle, IntPtr userData, UInt32 status, IntPtr frame)
        {
            try
            {
                if (frame == IntPtr.Zero)
                {
                    return;
                }

                //如需直接处理RGB数据，请参考以下流程
                NT_SP_VideoFrame video_frame = (NT_SP_VideoFrame)Marshal.PtrToStructure(frame, typeof(NT_SP_VideoFrame));

                NT_SP_VideoFrame pVideoFrame = new NT_SP_VideoFrame();

                pVideoFrame.format_ = video_frame.format_;
                pVideoFrame.width_ = video_frame.width_;
                pVideoFrame.height_ = video_frame.height_;

                pVideoFrame.timestamp_ = video_frame.timestamp_;
                pVideoFrame.stride0_ = video_frame.stride0_;
                pVideoFrame.stride1_ = video_frame.stride1_;
                pVideoFrame.stride2_ = video_frame.stride2_;
                pVideoFrame.stride3_ = video_frame.stride3_;

                Int32 argb_size = video_frame.stride0_ * video_frame.height_;

                pVideoFrame.plane0_ = Marshal.AllocHGlobal(argb_size);
                CopyMemory(pVideoFrame.plane0_, video_frame.plane0_, (UInt32)argb_size);


                if (playWnd.InvokeRequired)
                {
                    playWnd.BeginInvoke(set_video_frame_call_back_, status, pVideoFrame);
                }
                else
                {
                    set_video_frame_call_back_(status, pVideoFrame);
                }
            }
            catch (Exception ex)
            {
                Log4netHelper.Error(ex);
            }
        }

        private void SetAudioPCMFrameCallBack(IntPtr handle, IntPtr user_data,UInt32 status, IntPtr data, UInt32 size,Int32 sample_rate, Int32 channel, Int32 per_channel_sample_number)
        {
            try
            {
                if (data == IntPtr.Zero || size == 0)
                {
                    return;
                }

                IntPtr pcmData = Marshal.AllocHGlobal((Int32)size);
                CopyMemory(pcmData, data, (UInt32)size);

                if (playWnd.InvokeRequired)
                {
                    playWnd.BeginInvoke(set_audio_pcm_frame_call_back_, status, pcmData, size, sample_rate, channel, per_channel_sample_number);
                }
                else
                {
                    set_audio_pcm_frame_call_back_(status, pcmData, size, sample_rate, channel, per_channel_sample_number);
                }
            }
            catch (Exception ex)
            {
               Log4netHelper.Error(ex);
            }
        }

        private void SP_SDKRenderVideoFrameTimestampCallBack(IntPtr handle, IntPtr userData,UInt64 timestamp, UInt64 reserve1, IntPtr reserve2)
        {
            try
            {
                if (playWnd.InvokeRequired)
                {
                    playWnd.BeginInvoke(set_render_video_frame_timestamp_callback_, timestamp, reserve1, reserve2);
                }
                else
                {
                    set_render_video_frame_timestamp_callback_(timestamp, reserve1, reserve2);
                }
            }
            catch (Exception ex)
            {
                StopPlayback();
                Log4netHelper.Error(ex);
            }
        }

        private void SDKUserDataCallBack(IntPtr handle, IntPtr user_data,UInt32 data_type,IntPtr data,UInt32 size,UInt64 timestamp,UInt64 reserve1,Int64 reserve2,IntPtr reserve3)
        {
            try
            {
                byte[] bData = new byte[size];

                Marshal.Copy(data, bData, 0, (int)(size));

                string str_data = Encoding.UTF8.GetString(bData);

                if (playWnd.InvokeRequired)
                {
                    playWnd.BeginInvoke(set_user_data_call_back_, data_type, str_data, size, timestamp, reserve1, reserve2, reserve3);
                }
                else
                {
                    set_user_data_call_back_(data_type, str_data, size, timestamp, reserve1, reserve2, reserve3);
                }
            }
            catch (Exception ex)
            {
              Log4netHelper.Error(ex);
            }
        }
        #endregion



        private void StopPlayback()
        {
            try
            {
                if (player_handle_ == IntPtr.Zero) return;
                if (is_playing_)
                {
                    //NT_SP_Stop是老接口，如使用，请配合NT_SP_Start同步使用
                    //NTSmartPlayerSDK.NT_SP_Stop(player_handle_);
                    NTSmartPlayerSDK.NT_SP_StopPlay(player_handle_);
                    is_playing_ = false;
                    if (is_gdi_render_)
                    {
                        playWnd.Invalidate(); //清空最后一帧数据，如不加，默认保留最后一帧画面
                    }
                }

                if (cur_video_frame_.plane0_ == IntPtr.Zero) return;
                Marshal.FreeHGlobal(cur_video_frame_.plane0_);
                cur_video_frame_.plane0_ = IntPtr.Zero;
            }
            catch (Exception ex)
            {
                Log4netHelper.Error(ex);
            }
        }














       

        #region PlaybackWindowResized
        private void PlaybackWindowResized(Int32 width, Int32 height)
        {
            width_ = width;
            height_ = height;

            int left = playWnd.Left;
            int top = playWnd.Top;


            if (ManagerForm.Width < 200 || ManagerForm.Height < 200)
                return;

            int dw = 1;
            int dh = 1;
            CalRenderSize(ManagerForm.Width - 60, ManagerForm.Height - 150, width_, height_, ref dw, ref dh);

            playWnd.SetBounds(left, top, dw, dh);

            if (player_handle_ == IntPtr.Zero)
            {
                return;
            }

            NTSmartPlayerSDK.NT_SP_OnWindowSize(player_handle_, dw, dh);
        }

        private void CalRenderSize(int limtWidth, int limtHeight, int videoWidth, int videoHeight, ref int dw, ref int dh)
        {
            dw = 100;
            dh = 100;

            if (videoWidth < 1)
                videoWidth = 1;

            if (videoHeight < 1)
                videoHeight = 1;

            // 先按宽度缩放
            int y = limtWidth * videoHeight / videoWidth;

            // 如果缩放高度还大，那就按高度继续缩放
            if (y > limtHeight)
            {
                dh = limtHeight;
                dw = limtWidth * limtHeight / y;
            }
            else
            {
                dw = limtWidth;
                dh = y;
            }

            if (dw < 16)
                dw = 16;

            if (dh < 16)
                dh = 16;
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        /// <param name="frame"></param>
        private void SdkVideoFrameCallBack(UInt32 status, NT_SP_VideoFrame frame)
        {
            try
            {
                if (cur_video_frame_.plane0_ != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(cur_video_frame_.plane0_);
                    cur_video_frame_.plane0_ = IntPtr.Zero;
                }

                cur_video_frame_ = frame;

                ManagerForm.Invalidate();
            }
            catch (Exception ex)
            {
                Log4netHelper.Error(ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="status"></param>
        /// <param name="data"></param>
        /// <param name="size"></param>
        /// <param name="sample_rate"></param>
        /// <param name="channel"></param>
        /// <param name="per_channel_sample_number"></param>
        private void SdkAudioPcmFrameCallBack(UInt32 status, IntPtr data, UInt32 size,Int32 sample_rate, Int32 channel, Int32 per_channel_sample_number)
        {
            try
            {
                //这里拿到回调的PCM frame，进行相关操作（如自己播放）
                //label_debug.Text = per_channel_sample_number.ToString();

                //release
                Marshal.FreeHGlobal(data);
            }
            catch (Exception ex)
            {
              Log4netHelper.Error(ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="user_data"></param>
        /// <param name="event_id"></param>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <param name="param3"></param>
        /// <param name="param4"></param>
        /// <param name="param5"></param>
        /// <param name="param6"></param>
        private void SdkEventCallBack(IntPtr handle, IntPtr user_data,UInt32 event_id,Int64 param1,Int64 param2,UInt64 param3,[MarshalAs(UnmanagedType.LPStr)] String param4,[MarshalAs(UnmanagedType.LPStr)] String param5,IntPtr param6)
        {
            try
            {
                if (playWnd.InvokeRequired)
                {
                    playWnd.BeginInvoke(set_event_call_back_, event_id, param1, param2, param3, param4, param5, param6);
                }
                else
                {
                    set_event_call_back_(event_id, param1, param2, param3, param4, param5, param6);
                }
            }
            catch (Exception ex)
            {
                Log4netHelper.Error(ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="event_id"></param>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <param name="param3"></param>
        /// <param name="param4"></param>
        /// <param name="param5"></param>
        /// <param name="param6"></param>
        private void EventCallBack(UInt32 event_id,Int64 param1,Int64 param2,UInt64 param3,[MarshalAs(UnmanagedType.LPStr)] String param4,[MarshalAs(UnmanagedType.LPStr)] String param5,IntPtr param6)
        {
            try
            {
                if (!is_playing_ && !is_recording_)
                {
                    return;
                }

                String show_str = "";

                if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_PLAYBACK_REACH_EOS == event_id)
                {
                    StopPlayback();
                    return;
                }
                else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_RECORDER_REACH_EOS == event_id)
                {
                    return;
                }
                else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_PULLSTREAM_REACH_EOS == event_id)
                {
                    if (player_handle_ != IntPtr.Zero)
                    {
                        NTSmartPlayerSDK.NT_SP_StopPullStream(player_handle_);
                    }

                    return;
                }
                else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_DURATION == event_id)
                {
                    Int64 duration = param1;

                    show_str = GetHMSMsFormatStr(duration, false, false);

                    return;
                }
                else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_RTSP_STATUS_CODE == event_id)
                {
                    int status_code = (int)param1;

                    show_str = "RTSP incorrect status code received: " + status_code.ToString() + ", 请确保用户名/密码正确";

                    // MessageBox.Show(show_str);
                }

                if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTING == event_id
                    || (UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTION_FAILED == event_id
                    || (UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTED == event_id
                    || (UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_DISCONNECTED == event_id)
                {
                    connection_status_ = event_id;
                }

                if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_START_BUFFERING == event_id
                    || (UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_BUFFERING == event_id
                    || (UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_STOP_BUFFERING == event_id)
                {
                    buffer_status_ = event_id;

                    if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_BUFFERING == event_id)
                    {
                        buffer_percent_ = (Int32)param1;
                    }
                }

                if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_DOWNLOAD_SPEED == event_id)
                {
                    download_speed_ = (Int32)param1;
                }

                if (connection_status_ != 0)
                {
                    show_str += "链接状态: ";

                    if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTING == connection_status_)
                    {
                        show_str += "链接中";
                        ContionStatusChange?.Invoke(NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTING);
                    }
                    else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTION_FAILED == connection_status_)
                    {
                        show_str += "链接失败";
                        ContionStatusChange?.Invoke(NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTION_FAILED);
                    }
                    else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTED == connection_status_)
                    {
                        show_str += "链接成功";
                        ContionStatusChange?.Invoke(NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTED);
                    }
                    else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_DISCONNECTED == connection_status_)
                    {
                        show_str += "链接断开";
                        ContionStatusChange?.Invoke(NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTION_FAILED);
                    }
                }

                if (download_speed_ != -1)
                {
                    String ss = "  下载速度: " + (download_speed_ * 8 / 1000).ToString() + "kbps " + (download_speed_ / 1024).ToString() + "KB/s";

                    show_str += ss;
                }

                if (buffer_status_ != 0)
                {
                    show_str += "  缓冲状态: ";

                    if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_START_BUFFERING == buffer_status_)
                    {
                        show_str += "开始缓冲";
                    }
                    else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_BUFFERING == buffer_status_)
                    {
                        String ss = "缓冲中 " + buffer_percent_.ToString() + "%";
                        show_str += ss;
                    }
                    else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_STOP_BUFFERING == buffer_status_)
                    {
                        show_str += "结束缓冲";
                    }
                }
            }
            catch (Exception ex)
            {
                Log4netHelper.Error(ex);
            }


        }

        /// <summary>
        /// 视频时间
        /// </summary>
        /// <param name="time_ms"></param>
        /// <param name="is_append_ms"></param>
        /// <param name="is_display_time_ms"></param>
        /// <returns></returns>
        private String GetHMSMsFormatStr(Int64 time_ms, bool is_append_ms, bool is_display_time_ms)
        {
            Int64 remain_ms = time_ms % 1000;
            Int64 t_s = time_ms / 1000;

            Int64 h = t_s / 3600;
            Int64 r = t_s % 3600;

            Int64 m = r / 60;
            Int64 s = r % 60;

            String twss = "";
            try
            {
                if (h != 0)
                {
                    twss = Convert.ToString(h) + "时";
                }

                if (h != 0 || m != 0)
                {
                    twss = twss + Convert.ToString(m) + "分";
                }

                if (h != 0 || m != 0 || s != 0)
                {
                    twss = twss + Convert.ToString(s) + "秒";
                }

                if (is_append_ms)
                {
                    if (remain_ms != 0)
                    {
                        twss = twss + "." + Convert.ToString(remain_ms);
                    }
                }

                if (is_display_time_ms)
                {
                    twss = twss + "(" + Convert.ToString(time_ms) + "ms)";
                }
            }
            catch (Exception ex)
            {
                Log4netHelper.Error(ex);
            }

            return twss;
        }





    }
}
